package com.xuecheng.content.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.commons.model.vo.CoursePubVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CoursePub;
import freemarker.template.TemplateException;
import io.minio.errors.*;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CoursePubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程发布前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/coursePub")
public class CoursePubController {

        @Autowired
        private CoursePubService coursePubService;

        @GetMapping("/preview/{courseBaseId}")
        public ModelAndView preview(@PathVariable("courseBaseId") Long courseBaseId) {
                ModelAndView modelAndView = new ModelAndView();
                // 指定模板
                modelAndView.setViewName("learing_article");
                // 查询相关数据
                Map map = coursePubService.preview(courseBaseId);
                // 指定数据
                modelAndView.addAllObjects(map);
                return modelAndView;
        }

        /**
         * 根据id 发布课程
         * @param courseBaseId 课程id
         * @return vo
         */
        @PostMapping("/publish/{courseBaseId}")
        public ResponseResult publish(@PathVariable("courseBaseId") Long courseBaseId) throws Exception {
                return coursePubService.publish(courseBaseId);
        }

        /**
         * 查询所有发布的课程
         * @return CoursePubVo
         */
        @GetMapping("/findAll")
        public ResponseResult findAll(){
                LambdaQueryWrapper<CoursePub> qw = new LambdaQueryWrapper<>();
                qw.eq(CoursePub::getStatus,1);
                List<CoursePub> coursePubs = coursePubService.list(qw);
                List<CoursePubVo> coursePubVos = BeanHelper.copyWithCollection(coursePubs, CoursePubVo.class);
                return ResponseResult.okResult(coursePubVos);
        }

        /**
         * 根据id查询已经发布的课程
         * @param id 课程id
         * @return vo
         */
        @GetMapping("/{id}")
        public ResponseResult findById(@PathVariable("id") Long id){
                CoursePub coursePub = coursePubService.getById(id);
                CoursePubVo vo = BeanHelper.copyProperties(coursePub, CoursePubVo.class);
                return ResponseResult.okResult(vo);
        }

}
